#include "liveMedia.hh"
#include "BasicUsageEnvironment.hh"
#include "GroupsockHelper.hh"
#include "h264_live_video_server_media_subssion.hpp"
void announceURL(RTSPServer* rtspServer, ServerMediaSession* sms) {
  if (rtspServer == NULL || sms == NULL) return; // sanuty check

  UsageEnvironment& env = rtspServer->envir();

  env << "Play this stream using the URL ";
  if (weHaveAnIPv4Address(env)) {
    char* url = rtspServer->ipv4rtspURL(sms);
    env << "\"" << url << "\"";
    delete[] url;
    if (weHaveAnIPv6Address(env)) env << " or ";
  }
  if (weHaveAnIPv6Address(env)) {
    char* url = rtspServer->ipv6rtspURL(sms);
    env << "\"" << url << "\"";
    delete[] url;
  }
  env << "\n";
}
static void announceStream(RTSPServer* rtspServer, ServerMediaSession* sms,
			   char const* streamName, char const* inputFileName) {
  UsageEnvironment& env = rtspServer->envir();

  env << "\n\"" << streamName << "\" stream, from the file \""
      << inputFileName << "\"\n";
  announceURL(rtspServer, sms);
}
int main(int argc, char** argv)
{
    UsageEnvironment* env=nullptr;
    Boolean resume_first_source = False;
    TaskScheduler* scheduler = BasicTaskScheduler::createNew();
    env = BasicUsageEnvironment::createNew(*scheduler);
    UserAuthenticationDatabase* auth_db = nullptr;
    RTSPServer* rtsp_server = RTSPServer::createNew(*env,8554,auth_db);
    if(rtsp_server==nullptr)
    {
        *env << "Failed to create RTSP server: " << env->getResultMsg() << "\n";
        exit(1);
    }
    char const* description_string
    = "Session streamed by \"testOnDemandRTSPServer\"";
    char const* stream_name ="h264ESVideoTest";
    ServerMediaSession* sms = ServerMediaSession::createNew(*env,stream_name,stream_name,description_string);
    sms->addSubsession(H264LiveVideoServerMediaSubssion::createNew(*env,resume_first_source));
    rtsp_server->addServerMediaSession(sms);
    announceStream(rtsp_server,sms,stream_name,"camera stream");
    env->taskScheduler().doEventLoop();
    return 0;
}